import requests
import json
import hashlib
# 融联云信

class RHYX_note():

    def MD5_sign(self,cpcode,msg,mobiles,excode,templetid,key):   # 加密签名

        sign = cpcode + msg + mobiles + excode + templetid + key

        md5_sign = hashlib.md5(sign.encode(encoding='UTF-8')).hexdigest()

        return md5_sign.lower()



    def send_note(self):   # 短信发送
        url = 'https://rcsapi.wo.cn:8043/umcinterface/sendtempletmsg'
        headers = {'Content-Type':'application/json'}

        cpcode = ''   # 商户编码
        msg = ''        # 模板参数值
        mobiles = ''       # 手机号
        excode = ''       # 扩展码 为空时传空字符串””
        templetid = ''       # 模板短信ID
        key = ''       # 融合云信平台分配的私钥
        sign = self.MD5_sign(cpcode,msg,mobiles,excode,templetid,key)       # MD5签名 签名字符串为cpcode+ msg+mobiles+excode+templetid+key，其中key为融合云信平台分配的私钥，将签名字符串用MD5加密后转换为小写字符串。

        data = {"cpcode": cpcode,
                "msg": msg,
                "mobiles": mobiles,
                "excode": excode,
                "templetid": templetid,
                "sign": sign,}
        res = requests.post(url=url,data=json.dumps(data),headers=headers)
        print(res.json())
        if res.json()['resultcode'] == 0:
            print('成功')
        else:
            print('失败')


    def status_report_sync(self):  # 状态报告同步接口
        url = ''
        headers = {'Content-Type': 'text/plain'}

        state = ''  # 短信发送的状态报告码，0：成功，其余失败
        mobile = ''  # 手机号码
        taskid = ''  # 内部订单号，匹配状态报告使用，字母和数字的混合
        sortid = ''  # 长短信消息序号
        time = ''  # 短信状态报告到达时间，格式为yyyyMMddHHmmss

        data = {"state": state,
                "mobile": mobile,
                "taskid": taskid,
                "sortid": sortid,
                "time": time}
        res = requests.post(url=url, data=json.dumps(data), headers=headers)
        print(res.json()['status'])
        if res.json()['status'] == 0:
            print('成功')
        else:
            print('异常')

    def up_note_sync(self):  # 上行短信同步接口
        url = ''
        headers = {'Content-Type': 'text/plain'}

        fisp = ''  # 上行端口号
        mobile = ''  # 手机号码
        msg = ''  # 上行短信内容
        time = ''  # 短信状态报告到达时间，格式为yyyyMMddHHmmss

        data = {"fisp": fisp,
                "mobile": mobile,
                "msg": msg,
                "time": time}
        res = requests.post(url=url, data=json.dumps(data), headers=headers)
        print(res.json()['status'])
        if res.json()['status'] == 0:
            print('成功')
        else:
            print('异常')


RHYX_note().send_note()




